/** @noSelf **/
import {Trigger} from "./solar/w3ts/handles/trigger";
import {MapPlayer} from "./solar/w3ts/handles/player";
import DebugUtil from "./solar/util/DebugUtil";
import BaseUtil from "./solar/util/BaseUtil";
import {Unit} from "./solar/w3ts/handles/unit";
import Gui from "./Gui";
import Log from "./solar/common/Log";
import {Easing, Tween} from "./solar/tween";
import SyncUtil from "./solar/util/SyncUtil";


export default class AppTest {


    start() {
        this.solarTest();
    }


    solarTest() {
        DebugUtil.onChat("1", () => {
            DebugUtil.showText(BaseUtil.getGameTime() + "")
            let unit = new Unit(0, FourCC("hpea"), 0, 0, 0)
            SetUnitState(unit.handle, ConvertUnitState(0x12), 500 + GetUnitState(GetTriggerUnit(), ConvertUnitState(0x12)) * 100)
            print_r(handledef(unit.handle).reference)

            this.uiTest();
        })
        // this.hookTest();
        //test http
        this.httpJsonTest();
        this.tweenTest()

    }

    tweenTest() {
        const coords = {x: 0, y: 0}
        const tween = new Tween(coords) // 创建一个新的tween，修改“coords”。
            .to({x: 300, y: 200}, 500) //在500毫秒内移动到（300,200）。
            .easing(Easing.Quadratic.Out) // 使用缓和功能使动画平滑。
            .onUpdate(() => {
                DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, 'Tween:x=' + coords.x + ",y=" + coords.y);
            });
        //
        DebugUtil.onChat("2", () => {
            tween.start()
        })

    }

    uiTest() {
        SyncUtil.onSyncData("gui_test", p => {
            DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0,
                20, '接收到数据同步！触发的玩家为:' + GetPlayerName(p))
        })
        BaseUtil.forLocalPlayer(() => {//异步执行
            new Gui();
        })
        // new Gui();
    }

    hookTest() {
        print(log.path)
        let jassCreateUnit = CreateUnit;
        _G.CreateUnit = function (this: void, id: player, unitid: number, x: number, y: number, face: number): unit {
            let info = "你创建了单位id:" + GetPlayerId(id) + " unitid=" + unitid + " x,y=" + x + "," + y
            print(info)
            log.info(info)
            //禁用日志写文件
            Log.enable = false
            return jassCreateUnit(id, unitid, x, y, face)
        }
        let jassRemoveItem = RemoveItem;
        _G.RemoveItem = function (this: void, whichItem: item): void {
            let info = "移除了物品:" + GetItemName(whichItem) + " handle=" + whichItem;
            print(info)
            log.info(info)
            //禁用日志打印
            Log.enablePrint = false
            jassRemoveItem(whichItem)
        }
    }


    httpJsonTest() {
        let trigger = new Trigger()
        trigger.registerPlayerChatEvent(MapPlayer.fromIndex(0), "tj", true)
        trigger.addAction(() => {
            // @ts-ignore
            let result: any = JSON.stringify({a: [1, {b: 2}]})
            DebugUtil.showText(BaseUtil.getGameTime() + "[对象转Json字符串=]" + result)
            // @ts-ignore
            result = JSON.parse('{"a":[1,{"b":2}]}')
            DebugUtil.showText(BaseUtil.getGameTime() + "[Json字符串转对象=]" + result)
            print_r(result)
            let a: any[] = result['a']
            DebugUtil.showText(BaseUtil.getGameTime() + "[Json字符串转对象a[0]=]" + a[0])
            DebugUtil.showText(BaseUtil.getGameTime() + "[Json字符串转对象a[1][\"b\"]=]" + a[1]["b"])
            //test http
            // HttpRequest("www.baidu.com", "", httpResponseData => {
            //     DebugUtil.showText("http请求响应报文数据=" + httpResponseData)
            // });
        })
    }

}
